package cn.liang.hadoop.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

public class ShowFileStatus {
    private MiniDFSCluster cluster;
    private FileSystem fs;

    @Before
    public void setUp() throws IOException {
        Configuration conf = new Configuration();
        if(System.getProperty("test.build.data") == null) {
            System.getProperty("test.build.data", "/tmp");
        }
        cluster = new MiniDFSCluster(conf, 1, true, null);
        fs = cluster.getFileSystem();
        OutputStream out = fs.create(new Path("/dir/file"));
        out.write("content".getBytes("UTF-8"));
        out.close();
    }

    @After
    public void tearDown() throws IOException {
        if(fs != null) {
            fs.close();
        }
        if(cluster != null) {
            cluster.shutdown();
        }
    }

    @Test(expected = FileNotFoundException.class)
    public void throwsFileNotFoundForNonExistentFile() throws IOException {
        fs.getFileStatus(new Path("no-such-file"));
    }

    @Test
    public void fileStatus() throws IOException {
        Path file = new Path("/dir/file");
        FileStatus status = fs.getFileStatus(file);
        System.out.println(status.getPath().toUri().getPath());
        System.out.println(status.isDirectory());
        System.out.println(status.getLen());
        System.out.println(status.getModificationTime());
        System.out.println(status.getReplication());
        System.out.println(status.getBlockSize());
        System.out.println(status.getOwner());
        System.out.println(status.getGroup());
        System.out.println(status.getPermission().toString());
    }
}
